import * as types from '../actions/actionTypes'
import config from '../config'

const initState = {
    ajaxStatus: config.ajaxStatus.NONE
}

function actionTypeEndWithSuccess(type) {
    return type.substring(type.length - 8) === "_SUCCESS"
}

export default function ajaxReducer(state = initState, action) {
    if (action.type === types.AJAX_CALL_BEGIN) {
        return { ajaxStatus: config.ajaxStatus.INPROGRESS }
    } else if (action.type === types.AJAX_CALL_ERROR || actionTypeEndWithSuccess(action.type)) {
        return { ajaxStatus: config.ajaxStatus.COMPLETED }
    } else {
        return state
    }
}